package com.qf.web.servlet;

import com.qf.domain.EBookCategory;
import com.qf.domain.PageBean;
import com.qf.service.CategoryService;
import com.qf.service.PageBeanService;
import com.qf.service.impl.CategoryServiceImpl;
import com.qf.service.impl.PageBeanServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 2020/12/9 17:21
 *
 * @author：Lixubiao
 */
@WebServlet(urlPatterns = "/derekservlet")
public class DerekServlet extends BaseServlet {
    public String pageBean(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        String page_size = request.getParameter("pageSize");
        String page_num = request.getParameter("pageNum");
        Integer pageSize = new Integer(6);
        Integer pageNum = new Integer(1);
        if (!StringUtils.isEmpty(page_num)){
            pageNum=Integer.parseInt(page_num);
            if (pageNum<1){
                pageNum=1;
            }
        }
        if (!StringUtils.isEmpty(page_size)){
            pageSize=Integer.parseInt(page_size);
            if (pageSize<0||pageSize>10){
                pageSize=6;
            }
        }
        //获取分类信息放到request域中
        CategoryService categoryService=new CategoryServiceImpl();
        List<EBookCategory> categories=categoryService.findCategory();
        request.setAttribute("categories", categories);
        //获取分页信息，并放到request域中
        PageBeanService pageBeanService = new PageBeanServiceImpl();
        PageBean pageBean=pageBeanService.getPageBean(pageSize,pageNum);
        request.setAttribute("pageBean", pageBean);
        return "/index.jsp";
    }
}
